﻿namespace Cellfish.Toolbox.Facts.Tasks
{
    using System;

    using Cellfish.Test;
    using Cellfish.Toolbox.Tasks;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class ProgressFacts
    {
        [TestMethod]
        public void Given_null_handler_When_creating_ProgressSimple_then_ArgumentNullException_is_thrown()
        {
            Ensure.Throws<ArgumentNullException>(() => new ProgressSimple<int>(null));
        }

        [TestMethod]
        public void Given_ProgressSimple_When_reporting_progress_Then_handler_is_called()
        {
            int reported = 0;
            var progress = new ProgressSimple<int>(x => reported = x);
            progress.Report(42);
            Assert.AreEqual(42, reported);
        }

        [TestMethod]
        public void Given_ProgressSimple_When_reporting_progress_and_handler_throws_Then_exception_is_thrown()
        {
            var progress = new ProgressSimple<int>(_ => { throw new Exception(); });
            Ensure.Throws<Exception>(() => progress.Report(42));
        }

        [TestMethod]
        public void Given_null_handler_When_creating_ProgressSafe_then_ArgumentNullException_is_thrown()
        {
            Ensure.Throws<ArgumentNullException>(() => new ProgressSafe<int>(null));
        }

        [TestMethod]
        public void Given_ProgressSafe_When_reporting_progress_Then_handler_is_called()
        {
            int reported = 0;
            var progress = new ProgressSafe<int>(x => reported = x);
            progress.Report(42);
            Assert.AreEqual(42, reported);
            Assert.IsNull(progress.LastError);
        }

        [TestMethod]
        public void Given_ProgressSimple_When_reporting_progress_and_handler_throws_Then_exception_is_stored_in_progress_object()
        {
            var progress = new ProgressSafe<int>(_ => { throw new Exception(); });
            progress.Report(42);
            Assert.IsNotNull(progress.LastError);
        }
    }
}
